﻿#include "pro_dialog.h"
#include "ui_pro_dialog.h"
#include"mainwindow.h"
#include"process.h"
#include<QDebug>
#include<QMenu>
#include<QPoint>
#include<QTextCodec>
#include<QMessageBox>
Pro_Dialog::Pro_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Pro_Dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("Task Manager");
    this->Pro=new process;
    this->fresh();
    timer=new QTimer(this);
    timer->setInterval(1000);
    //connect(this->timer,SIGNAL(timeout()),this,SLOT(diaodu_choice()));
    ui->tableWidget_2->setContextMenuPolicy(Qt::CustomContextMenu);
    this->open_pro();
}

Pro_Dialog::~Pro_Dialog()
{
    delete ui;
}

void Pro_Dialog::diaodu_choice()
{
    switch (this->diaodu) {
    case 1:
        this->Pro->R_r();
         this->setWindowTitle("Current: R_R");
        break;

    case 2:
        this->Pro->Fcfs_Priority();
         this->setWindowTitle("Current: FCFS_Priority");
        break;

    case 3:
        this->Pro->Fcfs();
         this->setWindowTitle("Current: FCFS");
        break;
    default:
        break;
    }    
    this->open_pro();
}

void Pro_Dialog::fresh()
{
       ui->diaodu->clear();
       ui->timeslips->clear();
       ui->diaodu->insertItem(1,tr("Round_robin"));
       ui->diaodu->insertItem(2,tr("First Come First Serve && Priority"));
       ui->diaodu->insertItem(3,tr("First Come First Serve"));
       ui->timeslips->insertItem(0,tr("1s"));
       ui->timeslips->insertItem(1,tr("2s"));
       ui->timeslips->insertItem(2,tr("4s"));


}



void Pro_Dialog::pro_opt_sure()
{

        if(this->Pro->scheduling_mode==1)//轮转调度算法
    {
       // this->Pro->R_R(this->Pro->simulate_or_interrupt,this->Pro->operate_choice,temp.cputime,temp.priority,this->choose_pro);
    }
    else if(this->Pro->scheduling_mode==2)//先来先服务加优先级
    {
         this->Pro->Fcfs_Priority();
     }
    else if(this->Pro->scheduling_mode==3)//先来先服务
    {
         this->Pro->Fcfs();
    }
    //fresh();//刷新
}

void Pro_Dialog::insert()
{
    this->Pro->New_process(0,0,0,0);
    this->pro_opt_sure();
    this->open_pro();
    this->timer->start();
}

void Pro_Dialog::block()
{
    this->Pro->block_process(this->Pro->Find_pro_byId(this->choose_pro)->id);
    this->pro_opt_sure();
    this->open_pro();
    this->timer->start();
}

void Pro_Dialog::wake()
{
    this->Pro->wakeup_process(this->Pro->Find_pro_byId(this->choose_pro)->id);
    this->pro_opt_sure();
    this->open_pro();
    this->timer->start();
}

void Pro_Dialog::dele()
{
    if(this->Pro->Find_pro_byId(this->choose_pro)->state==2) this->Pro->Delete_process_byId(this->Pro->Find_pro_byId(this->choose_pro)->id,this->Pro->Find_pro_byId(this->choose_pro)->state);
    else this->Pro->Insert_finish(this->Pro->Delete_process_byId(this->Pro->Find_pro_byId(this->choose_pro)->id,this->Pro->Find_pro_byId(this->choose_pro)->state));

    this->pro_opt_sure();
    this->open_pro();
    this->timer->start();
}

void Pro_Dialog::on_tableWidget_2_customContextMenuRequested(const QPoint &pos)
{
    this->timer->stop();
    QMenu *mene_run=new QMenu(ui->tableWidget_2);//运行进程
    QMenu *mene_wait=new QMenu(ui->tableWidget_2);//等待进程
    QMenu *mene_ready=new QMenu(ui->tableWidget_2);//就绪进程
    QMenu *mene_block=new QMenu(ui->tableWidget_2);//阻塞进程
    QMenu *mene_finish=new QMenu(ui->tableWidget_2);//完成进程
    QAction *action_new=new QAction("new",ui->tableWidget_2);
    QAction *action_block=new QAction("block",ui->tableWidget_2);
    QAction *action_wake_up=new QAction("wake_up",ui->tableWidget_2);
    QAction *action_delete=new QAction("delete",ui->tableWidget_2);
    //mene2->addAction(action);
    mene_run->addAction(action_block);
    mene_run->addAction(action_delete);
    mene_run->addAction(action_new);
    mene_wait->addAction(action_delete);
    mene_wait->addAction(action_new);
    mene_block->addAction(action_wake_up);
    mene_block->addAction(action_new);
    mene_ready->addAction(action_delete);
    mene_ready->addAction(action_new);
    mene_finish->addAction(action_new);

    connect(action_new,SIGNAL(triggered(bool)),this,SLOT(insert()));
    connect(action_block,SIGNAL(triggered(bool)),this,SLOT(block()));
    connect(action_wake_up,SIGNAL(triggered(bool)),this,SLOT(wake()));
    connect(action_delete,SIGNAL(triggered(bool)),this,SLOT(dele()));


    if(ui->tableWidget_2->currentColumn()==0)
    {
        this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
        switch(this->Pro->Find_pro_byId(this->choose_pro)->state){
        case 1:
            this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
            mene_run->exec(QCursor::pos());
            break;
        case 2:
            this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
            mene_wait->exec(QCursor::pos());
            break;
        case 3:
            this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
            mene_ready->exec(QCursor::pos());
            break;
        case 4:
            this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
            mene_block->exec(QCursor::pos());
            break;
        case 5:
            this->choose_pro=ui->tableWidget_2->currentIndex().data(0).toInt();//进程id
            mene_finish->exec(QCursor::pos());
        default:
            break;
        }

    }
}

void Pro_Dialog::getfromfile(int type,int id)//id 是要删除的id
{
    if(type==0)//新建操作
    {
        this->Pro->New_process(1000,0,0,id);
        this->pro_opt_sure();
        this->open_pro();
        this->timer->start();

    }
    else if(type==1)//删除
    {


        if(this->Pro->Find_pro_byfileId(id)->state==2) this->Pro->Delete_process_byId(this->Pro->Find_pro_byfileId(id)->id,this->Pro->Find_pro_byfileId(id)->state);
        else this->Pro->Insert_finish(this->Pro->Delete_process_byId(this->Pro->Find_pro_byfileId(id)->id,this->Pro->Find_pro_byfileId(id)->state));

        this->pro_opt_sure();
        this->open_pro();
        this->timer->start();
    }

}


void Pro_Dialog::on_diaodu_currentIndexChanged(int index)
{
    if(index==0)
    {
        this->diaodu=1;
        this->Pro->scheduling_mode=1;
    }
    else if(index==1)
    {
        this->Pro->scheduling_mode=2;
        this->diaodu=2;
    }
     else if(index==2)
    {
        this->Pro->scheduling_mode=3;
        this->diaodu=3;
    }
}











//======================================================================显示设置
void Pro_Dialog::open_pro()
{
    //任务管理器
    ui->tableWidget_2->clear();
    QStringList header;
        header<<"  Process_ID "<<"  priority "<<"  cur_state "<<"  running_time  "<<"  rest_time  ";
                //
            ui->tableWidget_2->setRowCount(100);
            ui->tableWidget_2->setColumnCount(5);
                ui->tableWidget_2->setFrameShape(QFrame::NoFrame); //设置无边框
                ui->tableWidget_2->setShowGrid(false);
                ui->tableWidget_2->setSelectionBehavior(QAbstractItemView::SelectRows);
                ui->tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);
                    ui->tableWidget_2->setHorizontalHeaderLabels(header);
                    ui->tableWidget_2->resizeColumnsToContents();
                    ui->tableWidget_2->resizeRowsToContents();
                    ui->tableWidget_2->verticalHeader()->setVisible(false);//隐藏左边垂直
                //
        if(this->Pro->run_count!=0)
       {
            PPCB* run_p=this->Pro->runhead->next;
            for(int i=0;i<this->Pro->run_count;i++)
            {
                QString s;
                for(int j=0;j<5;j++)
                {
                    if(j==0)
                    {
                        s=QString::number(run_p->id,10);
                    }
                    else if(j==1)
                    {
                        s=QString::number(run_p->priority,10);
                    }
                    else if(j==2)
                    {
                        s="Running";
                    }
                    else if(j==3)
                    {
                        s=QString::number(run_p->cputime,10);
                    }
                    else if(j==4)
                    {
                        s=QString::number(run_p->lefttime,10);
                    }
                    ui->tableWidget_2->setItem(i,j,new QTableWidgetItem(s));

                }
                run_p=run_p->next;
            }
        }

        //===========================================================================================
        if(this->Pro->ready_count!=0)
       {
            PPCB* ready_p=this->Pro->readyhead->next;
            for(int i=0;i<this->Pro->ready_count;i++)
            {

                int t=i+this->Pro->run_count;
                QString s;
                for(int j=0;j<5;j++)
                {
                    if(j==0)
                    {
                        s=QString::number(ready_p->id,10);
                    }
                    else if(j==1)
                    {
                        s=QString::number(ready_p->priority,10);
                    }
                    else if(j==2)
                    {
                        s="ready";
                    }
                    else if(j==3)
                    {
                        s=QString::number(ready_p->cputime,10);
                    }
                    else if(j==4)
                    {
                        s=QString::number(ready_p->lefttime,10);
                    }
                    ui->tableWidget_2->setItem(t,j,new QTableWidgetItem(s));
                }
            ready_p=ready_p->next;
            }
        }

        //===========================================================================================
        if(this->Pro->wait_count!=0)
       {
            PPCB* wait_p=this->Pro->waithead->next;
            for(int i=0;i<this->Pro->wait_count;i++)
            {

                int t=i+this->Pro->run_count+this->Pro->ready_count;
                QString s;
                for(int j=0;j<5;j++)
                {
                    if(j==0)
                    {
                        s=QString::number(wait_p->id,10);
                    }
                    else if(j==1)
                    {
                        s=QString::number(wait_p->priority,10);
                    }
                    else if(j==2)
                    {
                        s="waiting";
                    }
                    else if(j==3)
                    {
                        s=QString::number(wait_p->cputime,10);
                    }
                    else if(j==4)
                    {
                        s=QString::number(wait_p->lefttime,10);
                    }
                    ui->tableWidget_2->setItem(t,j,new QTableWidgetItem(s));
                }
            wait_p=wait_p->next;
            }
        }
        //===========================================================================================
        if(this->Pro->finish_count!=0)
       {
            PPCB* finish_p=this->Pro->finishhead->next;
            for(int i=0;i<this->Pro->finish_count;i++)
            {

                int t=i+this->Pro->run_count+this->Pro->ready_count+this->Pro->wait_count;
                QString s;
                for(int j=0;j<5;j++)
                {
                    if(j==0)
                    {
                        s=QString::number(finish_p->id,10);
                    }
                    else if(j==1)
                    {
                        s=QString::number(finish_p->priority,10);
                    }
                    else if(j==2)
                    {
                        s="finish";
                    }
                    else if(j==3)
                    {
                        s=QString::number(finish_p->cputime,10);
                    }
                    else if(j==4)
                    {
                        s=QString::number(finish_p->lefttime,10);
                    }
                    ui->tableWidget_2->setItem(t,j,new QTableWidgetItem(s));
                }
            finish_p=finish_p->next;
            }
        }

        //===========================================================================================
        if(this->Pro->block_count!=0)
       {
            PPCB* finish_p=this->Pro->blockhead->next;
            for(int i=0;i<this->Pro->block_count;i++)
            {

                int t=i+this->Pro->run_count+this->Pro->ready_count+this->Pro->wait_count+this->Pro->finish_count;
                QString s;
                for(int j=0;j<5;j++)
                {
                    if(j==0)
                    {
                        s=QString::number(finish_p->id,10);
                    }
                    else if(j==1)
                    {
                        s=QString::number(finish_p->priority,10);
                    }
                    else if(j==2)
                    {
                        s="block";
                    }
                    else if(j==3)
                    {
                        s=QString::number(finish_p->cputime,10);
                    }
                    else if(j==4)
                    {
                        s=QString::number(finish_p->lefttime,10);
                    }
                    ui->tableWidget_2->setItem(t,j,new QTableWidgetItem(s));
                }
            finish_p=finish_p->next;
            }
        }



        this->timer->start();
}



void Pro_Dialog::on_timeslips_currentTextChanged(const QString &arg1)//=======================================更改时间片大小
{
    QString time;
    time=ui->timeslips->currentText();
    if(time=="1s")
    {
        this->Pro->timeslip=1;
        //this->timer->setInterval(1000);
    }
        else if(time=="2s")
    {
        this->Pro->timeslip=2;
        //this->timer->setInterval(2000);
    }
        else if(time=="4s")
    {
        this->Pro->timeslip=4;
        //this->timer->setInterval(4000);
    }
}
